package com.hao.shirojwt.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hao.shirojwt.util.json.JsonService;
import com.hao.shirojwt.util.json.impl.JacksonJsonServiceImpl;
import com.hao.shirojwt.util.lock.Lock;
import com.hao.shirojwt.util.lock.impl.RedisLockImpl;
import com.hao.shirojwt.util.thread.ThreadPoolService;
import com.hao.shirojwt.util.thread.impl.ThreadPoolServiceImpl;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class CustomConfiguration {

    @Bean
    public DbSearcher getDbSearcher() throws DbMakerConfigException, IOException {
        String dbPath = this.getClass().getResource("/ip2region.db").getFile();
        DbConfig config = new DbConfig();
        DbSearcher searcher = new DbSearcher(config, dbPath);
        return searcher;
    }

    @Bean
    public Lock lock() {
        return new RedisLockImpl();
    }

    @Bean
    public JsonService getJsonService(){
        //return new FastJsonServiceImpl();
        return new JacksonJsonServiceImpl();
    }

    @Bean
    public ObjectMapper getObjectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        return objectMapper;
    }

    @Bean
    public ThreadPoolService getThreadPoolService(){
        return new ThreadPoolServiceImpl();
    }
}
